package com.ruoyi.relationOrder.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.order.domain.Order;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.relationOrder.domain.DistributionOrderRelation;
import com.ruoyi.relationOrder.service.IDistributionOrderRelationService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 分销订单关联Controller
 * 
 * @author ruoyi
 * @date 2025-11-07
 */
@RestController
@RequestMapping("/ruoyi/relationOrder")
public class DistributionOrderRelationController extends BaseController
{
    @Autowired
    private IDistributionOrderRelationService distributionOrderRelationService;

    /**
     * 获取分销订单关联列表时，将分销状态为'1'（待结算）的优先级设为最高，排在最前面
     * 分销状态：0-待支付 1-待结算 2-已结算 3-已取消 4-已扣回'
     * 获取分销订单关联汇总列表
     */
    @GetMapping("/selectOrderRelationSummaryList")
    public TableDataInfo selectOrderRelationSummaryList(DistributionOrderRelation distributionOrderRelation) {
        startPage();
        List<DistributionOrderRelation> list = distributionOrderRelationService.selectOrderRelationSummaryList(distributionOrderRelation);
        return getDataTable(list);
    }

    /**
     * 查询分销订单关联列表
     */
    @PreAuthorize("@ss.hasPermi('ruoyi:relationOrder:list')")
    @GetMapping("/list")
    public TableDataInfo list(DistributionOrderRelation distributionOrderRelation)
    {
        startPage();
        List<DistributionOrderRelation> list = distributionOrderRelationService.selectDistributionOrderRelationList(distributionOrderRelation);
        return getDataTable(list);
    }

    /**
     * 导出分销订单关联列表
     */
    @PreAuthorize("@ss.hasPermi('ruoyi:relationOrder:export')")
    @Log(title = "分销订单关联", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DistributionOrderRelation distributionOrderRelation)
    {
        List<DistributionOrderRelation> list = distributionOrderRelationService.selectDistributionOrderRelationList(distributionOrderRelation);
        ExcelUtil<DistributionOrderRelation> util = new ExcelUtil<DistributionOrderRelation>(DistributionOrderRelation.class);
        util.exportExcel(response, list, "分销订单关联数据");
    }

    /**
     * 获取分销订单关联详细信息
     */
    @PreAuthorize("@ss.hasPermi('ruoyi:relationOrder:query')")
    @GetMapping(value = "/{relationId}")
    public AjaxResult getInfo(@PathVariable("relationId") Long relationId)
    {
        return success(distributionOrderRelationService.selectDistributionOrderRelationByRelationId(relationId));
    }

    /**
     * 新增分销订单关联
     */
    @PreAuthorize("@ss.hasPermi('ruoyi:relationOrder:add')")
    @Log(title = "分销订单关联", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DistributionOrderRelation distributionOrderRelation)
    {
        return toAjax(distributionOrderRelationService.insertDistributionOrderRelation(distributionOrderRelation));
    }

    /**
     * 修改分销订单关联
     */
    @PreAuthorize("@ss.hasPermi('ruoyi:relationOrder:edit')")
    @Log(title = "分销订单关联", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DistributionOrderRelation distributionOrderRelation)
    {
        return toAjax(distributionOrderRelationService.updateDistributionOrderRelation(distributionOrderRelation));
    }

    /**
     * 删除分销订单关联
     */
    @PreAuthorize("@ss.hasPermi('ruoyi:relationOrder:remove')")
    @Log(title = "分销订单关联", businessType = BusinessType.DELETE)
	@DeleteMapping("/{relationIds}")
    public AjaxResult remove(@PathVariable Long[] relationIds)
    {
        return toAjax(distributionOrderRelationService.deleteDistributionOrderRelationByRelationIds(relationIds));
    }

    /**
     * 执行结算任务
     */
    @GetMapping("/execute")
    public AjaxResult executeSettlement() {
        try {
            distributionOrderRelationService.processSettlementOrders();
            return AjaxResult.success("结算任务执行完成");
        } catch (Exception e) {
            return AjaxResult.error("结算任务执行失败：" + e.getMessage());
        }
    }
}
